import numpy as np
import matplotlib.pyplot as plt
from scipy.fftpack import fft
from matplotlib.lines import lineStyles

def FFT(Fs, data):
    """
    对输入信号进行FFT
    :param Fs:  采样频率
    :param data:待FFT的序列
    :return:
    """
    L = len(data)  # 信号长度
    N = np.power(2, np.ceil(np.log2(L)))  # 下一个最近二次幂，也即N个点的FFT
    result = np.abs(fft(x=data, n=int(N))) / L * 2  # N点FFT
    axisFreq = np.arange(int(N)) * Fs / N  # 频率坐标
    result = result[range(int(N))]  # 因为图形对称，所以取一半
    return axisFreq, result

file = open('output.res','r')
lines = file.readlines()

data = []
for line in lines:
    row = line.split()
    row_data = [float(x) for x in row]
    data.append(row_data)

time = np.array([row[0] for row in data])
es_res = np.array([row[1] for row in data])
rk4_res = np.array([row[2] for row in data])
aes_res = np.array([row[3] for row in data])
acc_res = np.array([row[4] for row in data])

#plt.plot(time,es_res,linestyle = 'dashed', color ='g', label = 'Euler')
plt.plot(time,rk4_res,linestyle = 'solid', color ='b', label = 'RK4')
plt.plot(time,aes_res,linestyle = 'dashed', color ='orange', label = 'Adv Euler')
plt.plot(time,acc_res,linestyle = 'dotted', color ='r', label = 'Acc')

plt.xlabel('time')
plt.ylabel('θ')
plt.legend()
plt.show()